<template>
  <div class="role-manage">
    <div class="query-form">
      <el-form-item>
       <span> {{year}} 年 {{month}} 月工资结构</span>
      </el-form-item>
    </div>
    <div class="base-table">
      <div class="action">
        <el-button type="primary" @click="handleAdd">创建</el-button>
      </div>
      <el-table :data="roleList">
        <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :formatter="item.formatter"
        >
        </el-table-column>
        <el-table-column label="操作" width="260">
          <template #default="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          class="pagination"
          background
          layout="prev, pager, next"
          :total="pager.total"
          :page-size="pager.pageSize"
          @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="新增" v-model="showModal">
      <el-form
          ref="dialogForm"
          :model="salaryForm"
          label-width="100px"
          :rules="rules">

        <el-form-item label="工资类型" prop="salaryTypeId">
          <el-select v-model="salaryForm.salaryTypeId" placeholder="请选择工资类型">
            <el-option
                v-for="salaryType in salaryTypeList"
                :key="salaryType.id"
                :label="salaryType.type"
                :value="salaryType.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import utils from "../utils/utils.js";

export default {
  name: "SalaryStructure",
  data() {
    return {
      columns: [
        {
          label: "序号",
          prop: "id",
        },
        {
          label: "字段名",
          prop: "salaryName",
        },
        {
          label: "工资类型名",
          prop: "salaryType",
        }
      ],
      roleList: [],
      salaryTypeList: [],
      monthTypeId: '',
      year: '',
      month: '',
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      showModal: false,
      action: "create",
      salaryForm: {},
      rules: {
        salaryTypeId: [
          {
            required: true,
            message: "请选择工资类型",
          },
        ],
      },
      // 权限展示
      showPermission: false,
      curRoleId: "",
      curRoleName: "",
      menuList: [],
      // 菜单映射表
      actionMap: {},
    };
  },
  created() {
    this.monthTypeId = this.$route.query.id
    this.year = this.$route.query.year
    this.month = this.$route.query.month
    this.salaryForm.monthTypeId = this.$route.query.id;
    this.getMonthSalaryList();
    this.getSalaryTypeList();
  },
  methods: {
    async getMonthSalaryList() {
      let param = {
        page: this.pager.pageNum,
        pageSize: this.pager.pageSize,
        monthTypeId: this.monthTypeId
      }
      let list= await this.$api.getMonthSalaryList(param);
      //console.log(list)
      this.roleList = list.rows;
      this.pager.total = list.records;
    },
    async getSalaryTypeList() {
      let list= await this.$api.getSalaryTypeList();
      this.salaryTypeList = list;
    },
    // 表单重置
    handleReset(form) {
      this.$refs[form].resetFields();
    },
    // 工资结构添加
    handleAdd() {
      this.action = "create";
      this.showModal = true;
    },
    // 工资结构编辑
    handleEdit(row) {
      this.action = "edit";
      this.showModal = true;
      this.$nextTick(() => {
        this.salaryForm = {
          id: row.id,
          monthTypeId: row.monthTypeId,
          salaryTypeId: row.salaryTypeId
        };
      });
    },
    // 工资结构删除
    async handleDel(id) {
      await this.$api.deleteMonthSalary(id);
      this.$message.success("删除成功");
      this.getMonthSalaryList();
    },
    // 弹框关闭
    handleClose() {
      this.handleReset("dialogForm");
      this.showModal = false;
    },
    // 工资结构提交
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          let { salaryForm, action } = this;
          let params = { ...salaryForm, action };
          if(this.action == "create") {
            await this.$api.addMonthSalary(params);
            this.$message.success("创建成功");
          } else if(this.action == "edit") {
            await this.$api.updateMonthSalary(params);
            this.$message.success("修改成功");
          }
          this.showModal = false;
          this.handleReset("dialogForm");
          this.getMonthSalaryList();
        }
      });
    },
    handleCurrentChange(current) {
      this.pager.pageNum = current;
      this.getMonthSalaryList();
    },

  },


}
</script>

<style scoped>

</style>